<?php

namespace app\index\controller;

use app\common\controller\Basecontroller;
use app\common\controller\Map;
use app\index\model\Classification;
use app\index\model\Coenvironment;
use app\index\model\Collection;
use app\index\model\Comassage;
use app\index\model\Comcoll;
use app\index\model\Company;
use app\index\model\Coproduct;
use app\index\model\Cotype;
use app\index\model\Examine;
use app\index\model\Official;
use app\index\model\Region;
use app\index\model\User;
use think\Db;
use think\db\Where;
use think\Request;
use function AlibabaCloud\Client\value;

class CompanyBack extends Basecontroller
{
    public function companyBack(Request $request,Company $company,Comcoll $comcoll,Map $map,Coenvironment $coenvironment,Coproduct $coproduct,Comassage $comassage)
    {
        $user = $this->getUserID();
        $companyId = $request->param('company_id');
        $checkIsFollow = json_encode($comcoll->isFollow($user,$companyId));
        $is_delete = Db::table('company')->where('company_id','=',$companyId)->field('is_delete')->find();
        if ($is_delete['is_delete'] == 1) return $this->returnJson(self::ERROR,'该公司已注销');
        $companyInfo = $company->getCompany($companyId);
        $companyEnviron = $coenvironment->getEnvironAttr($companyId);
        $companyProduct = $coproduct->getAllProductAttr($companyId);
        $companyMassage = $comassage->getCompanyMassageAttr($companyId);
        if (empty($companyMassage)) return $this->returnJson(self::OK,'该公司信息错误');
        if ($checkIsFollow == 'null'){
            return $this->returnJson(self::OK,'返回公司信息',[$companyInfo,$companyMassage,$companyEnviron,$companyProduct,'该用户未关注']);
        }else{
            return $this->returnJson(self::OK,'返回公司信息',[$companyInfo,$companyMassage,$companyEnviron,$companyProduct,'该用户已关注']);
        }
    }

    public function userCompany(Company $company,User $user)
    {
        $userId = $this->getUserID();
        $companyId =  $user->getUserCompanyAttr($userId);
        $companyLocation =  $company->getUserPosition($companyId);
        return $this->returnJson(self::OK,'返回公司人事信息',$companyLocation);
    }


    public function Follow(Request $request,Comcoll $comcoll)
    {
        $user = $this->getUserID();
        $companyId = $request->param('company_id');
        $insetFollow = [
            'user_id' => $user,
            'company_id' => $companyId
        ];
        $checkFollow = json_encode($comcoll->isFollow($user,$companyId));
        if ($checkFollow != null){
            $comcoll->updateFollow($user,$companyId);
            return $this->returnJson(self::OK,'关注成功');
        }
        $comcoll->Follow($insetFollow);
        return $this->returnJson(self::OK,'关注成功');
    }
    public function UnFollow(Request $request,Comcoll $comcoll)
    {
        $user = $this->getUserID();
        $companyId = $request->param('company_id');
        $comcoll->UnFollow($user,$companyId);
        return $this->returnJson(self::OK,'取消关注成功');
    }

    public function companyPosition(Request $request,Official  $official)
    {
        $companyId = $request->param('company_id');
        $companyMassage = json_encode($official->getCompanyPosition($companyId));
        if ($companyMassage == '[]') return $this->returnJson(self::ERROR,'该公司没有发布职位');
        return $this->returnJson(self::OK,'返回公司的职位信息',$official->getCompanyPosition($companyId));
    }
    public function HotCompanyAreaBack(Region $region)
    {
        $companyAreaBack =  $region->getReginAttr();
        return $this->returnJson(self::OK,'返回省',$companyAreaBack);
    }

    public function hotOfficial(Request $request,Examine $examine)
    {
        $type = $request->param('type');
        $city = $request->param('area');
        $education = $request->param('edu');
        $hotOffBack =  $examine->HotOffBack($type,$city,$education);
        return $this->returnJson(self::OK,'返回热招职位',$hotOffBack);
    }

    public function apply(Request $request,Examine $examine)
    {
        $officialId = $request->param('off_id');
        $userId = $this->getUserID();
        $backApply = [];
        foreach ($officialId as $key => $value){
            $checkExamine = $examine->isApply($userId,$value);
            $backApply[$key] = $checkExamine;
        }
        if(empty($checkExamine)){
            return $this->returnJson(self::OK,'该用户没有应聘热门公司中的公司','没有应聘的公司');
        }else{
            return $this->returnJson(self::OK,'返回该用户应聘热门公司中的公司',$backApply);
        }
    }

}